import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import '../../resource/language/generated/l10n.dart';
import 'themes.dart';
import 'toast.dart';

///通用弹窗
Future<void> showRideCommonDialog({
  required BuildContext context,
  Widget? icon,
  required String? title,
  required Widget content,
  bool cancelable = false,
  String? confirmText,
  String? cancelText,
  required VoidCallback onConfirm,
  bool showCancel = true,
  VoidCallback? onCancel,
  VoidCallback? protocalClick,
}) async {
  showDialog<void>(
      context: context,
      barrierDismissible: cancelable,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          child: SingleChildScrollView(
            child: Container(
              decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(16)),
                color: Colors.white,
                image: DecorationImage(
                  image: AssetImage('assets/images/bg_dialog.png'),
                  fit: BoxFit.cover,
                ),
              ),
              width: 279,
              padding: const EdgeInsets.symmetric(horizontal: 26, vertical: 13),
              alignment: Alignment.topCenter,
              child: Column(
                children: [
                  if (icon != null)
                    Padding(
                      padding: const EdgeInsets.only(bottom: 16),
                      child: icon,
                    ),
                  title == null || title.isEmpty
                      ? Container()
                      : Text(
                          title,
                          style: const TextStyle(
                              color: Colors.black,
                              fontSize: 17,
                              fontWeight: FontWeight.bold),
                        ),
                  const SizedBox(height: 23),
                  InkWell(
                    onTap: () {
                      Navigator.pop(context);
                      protocalClick?.call();
                    },
                    child: ConstrainedBox(
                      constraints: const BoxConstraints(maxHeight: 200),
                      child: content,
                    ),
                  ),
                  const SizedBox(height: 13),
                  // RButton(
                  //   title: confirmText ?? T.current!.surer,
                  //   onPressed: () {
                  //     Navigator.pop(context);
                  //     onConfirm();
                  //   },
                  // ),
                  if (showCancel)
                    const SizedBox(
                      height: 16,
                    ),
                  if (showCancel)
                    InkWell(
                      onTap: () {
                        Navigator.pop(context);
                        onCancel?.call();
                      },
                      child: Text(
                        cancelText ?? T.current.cancel,
                        style: const TextStyle(
                          color: Colors.black,
                          fontSize: 14,
                        ),
                      ),
                    ),
                ],
              ),
            ),
          ),
        );
      }).then(
    (value) {
      if (cancelable) {
        onCancel?.call();
      }
    },
  );
}

//弹窗 确定按钮高亮 取消按钮浅色 左右排列
Future<bool?> showNormalDialog({
  required BuildContext context,
  required String? title,
  required Widget content,
  bool cancelable = false,
  String? confirmText,
  String? cancelText,
  required VoidCallback onConfirm,
  bool showCancel = true,
  VoidCallback? onCancel,
}) async {
  return showDialog<bool?>(
      context: context,
      barrierDismissible: cancelable,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.transparent,
          child: SingleChildScrollView(
            child: Container(
              decoration: const BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(16)),
                color: Colors.white,
                image: DecorationImage(
                  image: AssetImage('assets/images/bg_dialog.png'),
                  fit: BoxFit.cover,
                ),
              ),
              width: 279,
              padding: const EdgeInsets.only(
                  left: 21, right: 21, top: 22, bottom: 20),
              alignment: Alignment.topCenter,
              child: Column(
                children: [
                  title == null || title.isEmpty
                      ? Container()
                      : Text(
                          title,
                          style: const TextStyle(
                              color: Colors.black,
                              fontSize: 16,
                              fontWeight: FontWeight.w500),
                        ),
                  Container(
                    padding: const EdgeInsets.only(top: 17, bottom: 24),
                    child: content,
                  ),
                  Row(
                    mainAxisSize: MainAxisSize.max,
                    children: [
                      Expanded(
                        flex: 1,
                        child: InkWell(
                          onTap: () {
                            Navigator.maybePop(context, true);
                            onConfirm();
                          },
                          child: Container(
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(21),
                              color: RColors.buttonYellow,
                            ),
                            height: 42,
                            alignment: Alignment.center,
                            child: Text(
                              confirmText ?? T.current!.surer,
                              style: const TextStyle(
                                color: RColors.textBlack,
                                fontSize: 15,
                              ),
                            ),
                          ),
                        ),
                      ),
                      if (showCancel)
                        const SizedBox(
                          width: 17,
                        ),
                      if (showCancel)
                        Expanded(
                          flex: 1,
                          child: InkWell(
                            onTap: () {
                              Navigator.maybePop(context, false);
                              onCancel?.call();
                            },
                            child: Container(
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(21),
                                  color: const Color(0xFFF6F7F9)),
                              height: 42,
                              alignment: Alignment.center,
                              child: Text(
                                cancelText ?? T.current!.cancel,
                                style: const TextStyle(
                                  color: Color(0xFF999999),
                                  fontSize: 15,
                                ),
                              ),
                            ),
                          ),
                        ),
                    ],
                  )
                ],
              ),
            ),
          ),
        );
      });
}

Future<void> showAlertDialog({
  required BuildContext context,
  Widget? icon,
  required String? title,
  required Widget content,
  String? confirmTittle,
  bool cancelable = false,
  required VoidCallback onConfirm,
  bool showCancel = true,
  VoidCallback? onCancel,
}) async {
  showDialog<void>(
      context: context,
      barrierDismissible: cancelable,
      builder: (BuildContext context) {
        return Dialog(
          backgroundColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.0),
          ),
          child: SingleChildScrollView(
            child: Container(
              width: 279,
              padding: const EdgeInsets.symmetric(horizontal: 25, vertical: 20),
              alignment: Alignment.topCenter,
              child: Column(
                children: [
                  if (icon != null)
                    Padding(
                      padding: const EdgeInsets.only(bottom: 16),
                      child: icon,
                    ),
                  title == null || title.isEmpty
                      ? Container()
                      : Text(
                          title,
                          style: const TextStyle(
                              color: Color(0xff333333),
                              fontSize: 16,
                              fontWeight: FontWeight.w500),
                        ),
                  const SizedBox(height: 18),
                  content,
                  const SizedBox(height: 18),
                  // RButton(
                  //   // width: 130,
                  //   height: 52,
                  //   title: confirmTittle ?? T.current!.i_know,
                  //   style: const TextStyle(
                  //       color: Color(0xff333333),
                  //       fontSize: 16,
                  //       fontWeight: FontWeight.w500),
                  //   onPressed: () {
                  //     Navigator.pop(context);
                  //     onConfirm();
                  //   },
                  // ),
                ],
              ),
            ),
          ),
        );
      }).then((value) {
    if (cancelable) {
      onCancel?.call();
    }
  });
}

///toast提示
showToast(String msg) {
  Toast.show(msg, Get.context!);
}

///底部菜单
Future<void> showRideBottomSheet(BuildContext context, List<String> titleList,
    IndexedWidgetBuilder onItemClick) {
  return showModalBottomSheet(
    shape: const RoundedRectangleBorder(
      //这里是modal的边框样式
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(8),
        topRight: Radius.circular(8),
      ),
    ),
    context: context,
    builder: (context) {
      return ListView.separated(
        shrinkWrap: true,
        itemBuilder: (context, index) {
          var listTitle = titleList[index];
          return ListTile(
            title: Center(
              child: Text(
                listTitle,
                style: const TextStyle(fontSize: 17, color: Colors.black),
              ),
            ),
            onTap: () {
              onItemClick(context, index);
            },
          );
        },
        itemCount: titleList.length,
        separatorBuilder: (BuildContext context, int index) {
          if (index == titleList.length - 2) {
            return const Divider(
              color: Color(0xFFF5F5F5),
              thickness: 8,
            );
          } else {
            return const Divider(
              color: Color(0xFFF5F5F5),
              thickness: 1,
            );
          }
        },
      );
    },
  );
}

bool _hasSetLoadingCallback = false;

Timer? _loadingTimer;

///显示全局加载框
showLoadingDialog({String? status}) async {
  await EasyLoading.show(status: status ??= T.current!.loading);
  if (!_hasSetLoadingCallback) {
    _hasSetLoadingCallback = true;

    /// 超时10秒消失
    EasyLoading.addStatusCallback((status) {
      if (status == EasyLoadingStatus.show) {
        _loadingTimer?.cancel();
        _loadingTimer = Timer.periodic(const Duration(seconds: 10), (timer) {
          _loadingTimer?.cancel();
          dismissLoadingDialog();
        });
      } else {
        _loadingTimer?.cancel();
        _loadingTimer = null;
      }
    });
  }
}

///关闭全局加载框
dismissLoadingDialog() async {
  await EasyLoading.dismiss();
}
